# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import time

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import os
class MjttPipeline:
    def open_spider(self, spider):
        pass
    def process_item(self, item, spider):
        with open(f'./数据/{item["title"]}','wb') as f:
            f.write(item["ts_con"])
        print(f'{item["title"]}下载完成')
        return item
    def close_spider(self, spider):
        print('等待10s')
        time.sleep(10)
        print('开始拼接视频')
        all_items = os.listdir('./数据/')
        ts = open('./数据/视频.ts', 'ab')
        for i in all_items:
            with open(f'./数据/{i}', 'rb') as f:
                aont = f.read()
            ts.write(aont)
            print(f'{i}写入完成')
        ts.close()
